package com.zerosky.crm.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zerosky.crm.dao.PersonalWorkPlanDAO;
import com.zerosky.crm.po.PersonalWorkPlan;
import com.zerosky.crm.service.PersonalWorkPlanService;
import com.zerosky.crm.util.PageInfo;

/**
 * PersonalWorkPlan业务逻辑实现类
 * 
 * @author yanglingchao;
 * @since 2017-07-31
 *
 */
@Service(value="personalWorkPlanService")
public class PersonalWorkPlanServiceImpl implements PersonalWorkPlanService {

	@Autowired
	private PersonalWorkPlanDAO personalWorkPlanDAO; // PersonalWorkPlanDAO数据接口

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public PageInfo<PersonalWorkPlan> getPersonalWorkPlanList(Map map, PageInfo<PersonalWorkPlan> page) {
		int begin = ((page.getPageNo() - 1) * page.getPageSize());
		int pageSize = page.getPageSize();

		// 写入数据
		map.put("pageNo", begin);
		map.put("pageSize", pageSize);
		List<PersonalWorkPlan> list = personalWorkPlanDAO.getPersonalWorkPlanList(map);
		page.setList(list);

		int count = personalWorkPlanDAO.countPersonalWorkPlan(map);// 获取总记录
		page.setPageCount(count);
		return page;
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public PersonalWorkPlan getPersonalWorkPlanById(String id) {
		return personalWorkPlanDAO.getPersonalWorkPlanById(id);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public Integer insertOrUpdate(PersonalWorkPlan personalWorkPlan, String operation) throws Exception {
		if ("新增".equalsIgnoreCase(operation)) {
			return personalWorkPlanDAO.insertPersonalWorkPlan(personalWorkPlan);
		} else if ("修改".equalsIgnoreCase(operation)) {
			return personalWorkPlanDAO.updatePersonalWorkPlan(personalWorkPlan);
		}
		return 0;
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public PageInfo<PersonalWorkPlan> getTodayPersonalWorkPlanList(Map<String, Object> map,
			PageInfo<PersonalWorkPlan> pageInfo) {
		// 写入数据
		List<PersonalWorkPlan> list = personalWorkPlanDAO.getPersonalWorkPlanList(map);
		pageInfo.setList(list);
		return pageInfo;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public int deletePersonalWorkPlan(int id) throws Exception {
		return personalWorkPlanDAO.deletePersonalWorkPlan(id);
	}
}
